package sk.tuke.constraintbuilder.handler;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;

import sk.tuke.constraintbuilder.editor.EditorInput;
import sk.tuke.constraintbuilder.editor.MultiPageEditor;
import sk.tuke.constraintbuilder.model.Constraint;
import sk.tuke.constraintbuilder.model.Model;
import sk.tuke.constraintbuilder.model.RootElement;
import sk.tuke.constraintbuilder.model.Ruleset;
import sk.tuke.constraintbuilder.model.Template;
import sk.tuke.constraintbuilder.views.ConstraintExplorerView;

public class CallEditor extends AbstractHandler {
	
	public static final String COMMAND = "sk.tuke.constraintbuilder.command.openEditor";
	public static final String PARAMETER = "sk.tuke.constraintbuilder.commandParameter.openEditorParam";
	
	public static final String NEW_CONSTRAINT = "newconstraint";
	public static final String NEW_TEMPLATE = "newtemplate";
	public static final String NEW_RULESET = "newruleset";
	public static final String EDIT = "edit";

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {

		// Get the view
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
		IWorkbenchPage page = window.getActivePage();
		ConstraintExplorerView listView = (ConstraintExplorerView) page.findView(ConstraintExplorerView.ID);
		
		String param = event.getParameter("sk.tuke.constraintbuilder.commandParameter.openEditorParam");
		if(param.equals(NEW_CONSTRAINT)){
			try {
				EditorInput newEditorInput = new EditorInput(new Constraint());
//				EditorInput newEditorInput = new EditorInput(EditorInput.EMPTY_CONSTRAINT);
				page.openEditor(newEditorInput, MultiPageEditor.ID);
			} catch (PartInitException e) {
				throw new RuntimeException(e);
			}
		}
		
		else if(param.equals(NEW_TEMPLATE)){
			try {
//				EditorInput newEditorInput = new EditorInput(EditorInput.EMPTY_TEMPLATE);
				EditorInput newEditorInput = new EditorInput(new Template());
				page.openEditor(newEditorInput, MultiPageEditor.ID);
			} catch (PartInitException e) {
				throw new RuntimeException(e);
			}
		}
		
		else if(param.equals(NEW_RULESET)){
			try {
//				EditorInput newEditorInput = new EditorInput(EditorInput.EMPTY_RULESET);
				EditorInput newEditorInput = new EditorInput(new Ruleset());
				page.openEditor(newEditorInput, MultiPageEditor.ID);
			} catch (PartInitException e) {
				throw new RuntimeException(e);
			}
		}
		
		else{
			try{
				int id = Integer.parseInt(param);
				RootElement inputElement = Model.getModel().getElementById(id);
				if(inputElement!=null){
					EditorInput input = new EditorInput(inputElement);
					try {
						page.openEditor(input, MultiPageEditor.ID);
					} catch (PartInitException e) {
						throw new RuntimeException(e);
					}
				}
			}
			catch(NumberFormatException e){
				return null;
			}
		}
		

		return null;
	}

}